using Blog2025.Application.Contracts.Interface;
using Blog2025.Domain.Filters;
using Blog2025.Domain.Repositories;
using Blog2025.Infrastructure.Data;
using Blog2025.Infrastructure.Filters;
using Blog2025.Infrastructure.Jwt;
using Blog2025.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Blog2025.Infrastructure;

public static class ServiceCollectionExtenstion
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration couiguration)
    {
        var connectionString = couiguration.GetConnectionString("pgsql");

        // 注册DbContext，使用PostgreSQL
        services.AddDbContext<Blog2025DbContext>(x =>
        {
            x.UseNpgsql(connectionString);
        });
        // 注册仓储实现
        services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
        // 过滤器
        services.AddScoped(typeof(IFilter<>), typeof(GenericFilter<>));
        // Jwt安全验证
        services.AddSingleton<IJwtService,JwtService>();

        return services;
    }
}